package com.taocore.top.title.action;

import java.util.List;
import java.util.logging.Logger;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.taobao.api.domain.Item;
import com.taobao.api.response.ItemUpdateResponse;
import com.taocore.top.title.domain.BatchChange;
import com.taocore.top.title.domain.DeleteSpace;
import com.taocore.top.title.domain.Prefix;
import com.taocore.top.title.domain.Replacement;
import com.taocore.top.title.domain.Suffix;
import com.taocore.top.title.service.TaobaoService;

@Results({
	@Result(name="success", type="httpheader")
})
public class BatchUpdateTitleAction extends ActionBase{

	static Logger _log = Logger.getLogger(ActionBase.class.getName());

	public static final int MAX_TITLE_BYTES = 60;
	
	public static final int SCOPE_ALL = 1;
	public static final int SCOPE_CATEGORY = 2;
	public static final int SCOPE_SPECIFIED = 3;
	
	private Replacement replacement;
	private Prefix prefix;
	private Suffix suffix;
	private DeleteSpace deleteSpace;
	private int scope = SCOPE_ALL;
	private String sellerCids;
	private String numIids;
	
	public String execute() throws Exception
	{
		BatchChange change = getChange();
		_log.info("change: " + change);
		if (change != null)
		{
			if (scope == SCOPE_ALL)
			{
				updateAll(change);
			}
			else if (scope == SCOPE_CATEGORY)
			{
				updateCategoies(change);
			}
			else if (scope == SCOPE_SPECIFIED)
			{
				updateSpecified(change);
			}
			return SUCCESS;
		}
		return ERROR;
	}

	private void updateAll(BatchChange change) throws Exception
	{
		String session = getSessionId();
		TaobaoService service = new TaobaoService();
		String fields = "num_iid,title";
		List<Item> onsaleItems = service.getAllOnsaleItems(fields, null, null, null, session);
		for (Item item : onsaleItems)
		{
			String newTitle = change.update(item.getTitle());
			_log.info("new title: " + newTitle);
			_log.info("bytes: " + newTitle.getBytes("GBK").length);
			if (newTitle.getBytes().length > MAX_TITLE_BYTES)
			{
				_log.warning("title is too long");
			}
			else
			{
				if (!item.getTitle().equals(newTitle))
				{
					ItemUpdateResponse rsp = service.updateTitle(item.getNumIid(), newTitle, session);
					if (rsp.isSuccess())
					{
						
					}
					else
					{
						_log.warning(TaobaoService.getError(rsp));
					}
				}
			}
		}
		List<Item> inventoryItems = service.getAllInventoryItems(fields, null, null, null, session);
		for (Item item : inventoryItems)
		{
			String newTitle = change.update(item.getTitle());
			_log.info("new title: " + newTitle);
			_log.info("bytes: " + newTitle.getBytes("GBK").length);
			if (newTitle.getBytes().length > MAX_TITLE_BYTES)
			{
				_log.warning("title is too long");
			}
			else
			{
				if (!item.getTitle().equals(newTitle))
				{
					ItemUpdateResponse rsp = service.updateTitle(item.getNumIid(), newTitle, session);
					if (rsp.isSuccess())
					{
						
					}
					else
					{
						_log.warning(TaobaoService.getError(rsp));
					}
				}
			}
		}
	}
	
	private void updateCategoies(BatchChange change) throws Exception
	{
		String session = getSessionId();
		TaobaoService service = new TaobaoService();
		String fields = "num_iid,title";
		List<Item> onsaleItems = service.getAllOnsaleItems(fields, null, sellerCids, null, session);
		for (Item item : onsaleItems)
		{
			String newTitle = change.update(item.getTitle());
			_log.info("new title: " + newTitle);
			_log.info("bytes: " + newTitle.getBytes("GBK").length);
			if (newTitle.getBytes().length > MAX_TITLE_BYTES)
			{
				_log.warning("title is too long");
			}
			else
			{
				if (!item.getTitle().equals(newTitle))
				{
					ItemUpdateResponse rsp = service.updateTitle(item.getNumIid(), newTitle, session);
					if (rsp.isSuccess())
					{
						
					}
					else
					{
						_log.warning(TaobaoService.getError(rsp));
					}
				}
			}
		}
		List<Item> inventoryItems = service.getAllInventoryItems(fields, null, sellerCids, null, session);
		for (Item item : inventoryItems)
		{
			String newTitle = change.update(item.getTitle());
			_log.info("new title: " + newTitle);
			_log.info("bytes: " + newTitle.getBytes("GBK").length);
			if (newTitle.getBytes().length > MAX_TITLE_BYTES)
			{
				_log.warning("title is too long");
			}
			else
			{
				if (!item.getTitle().equals(newTitle))
				{
					ItemUpdateResponse rsp = service.updateTitle(item.getNumIid(), newTitle, session);
					if (rsp.isSuccess())
					{
						
					}
					else
					{
						_log.warning(TaobaoService.getError(rsp));
					}
				}
			}
		}
	}
	
	private void updateSpecified(BatchChange change) throws Exception
	{
		String session = getSessionId();
		TaobaoService service = new TaobaoService();
		String fields = "num_iid,title";
		List<Item> items = service.getAllItems(numIids, fields, session);
		for (Item item : items)
		{
			String newTitle = change.update(item.getTitle());
			_log.info("new title: " + newTitle);
			_log.info("bytes: " + newTitle.getBytes("GBK").length);
			if (newTitle.getBytes().length > MAX_TITLE_BYTES)
			{
				_log.warning("title is too long");
			}
			else
			{
				if (!item.getTitle().equals(newTitle))
				{
					ItemUpdateResponse rsp = service.updateTitle(item.getNumIid(), newTitle, session);
					if (rsp.isSuccess())
					{
						
					}
					else
					{
						_log.warning(TaobaoService.getError(rsp));
					}
				}
			}
		}
	}
	
	public void setReplacement(Replacement replacement) {
		this.replacement = replacement;
	}

	public Replacement getReplacement() {
		return replacement;
	}

	public void setPrefix(Prefix prefix) {
		this.prefix = prefix;
	}

	public Prefix getPrefix() {
		return prefix;
	}

	public void setSuffix(Suffix suffix) {
		this.suffix = suffix;
	}

	public Suffix getSuffix() {
		return suffix;
	}

	public void setDeleteSpace(DeleteSpace deleteSpace) {
		this.deleteSpace = deleteSpace;
	}

	public DeleteSpace getDeleteSpace() {
		return deleteSpace;
	}

	public BatchChange getChange()
	{
		if (replacement != null)
		{
			return replacement;
		}
		else if (prefix != null)
		{
			return prefix;
		}
		else if (suffix != null)
		{
			return suffix;
		}
		else
		{
			deleteSpace = new DeleteSpace();
			return deleteSpace;
		}
	}

	public void setScope(int scope) {
		this.scope = scope;
	}

	public int getScope() {
		return scope;
	}

	public void setSellerCids(String sellerCids) {
		this.sellerCids = sellerCids;
	}

	public String getSellerCids() {
		return sellerCids;
	}

	public void setNumIids(String numIids) {
		this.numIids = numIids;
	}

	public String getNumIids() {
		return numIids;
	}

}
